package com.etpjava.action;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.etpjava.model.Pay;
import com.etpjava.service.SalaryService;
import com.etpjava.service.impl.SalaryServiceImpl;
import com.etpjava.util.GetSth;
import com.etpjava.util.Page;
import com.opensymphony.xwork2.ActionSupport;

/**
 * SalaryAction
 * 
 * @author liuk
 * @version 0.1.1
 */
public class SalaryAction extends ActionSupport {

	private static final long serialVersionUID = 2495033449345977427L;
	private Pay pay;
	private List<Pay> pays;
	private Integer id;
	// 删除多个id
	private int[] ids;

	private Page pagelist;

	SalaryService salaryService = new SalaryServiceImpl();

	// s* is for search

	private Integer minid;

	private Integer maxid;

	// 姓名
	private String sname;
	// 工龄
	private String sminage;
	// 工龄
	private String smaxage;
	// 发工资时间
	private String sminpt;
	// 发工资时间
	private String smaxpt;

	// 基本工资
	private String sminbase;
	private String smaxbase;

	// 删除
	public String delete() {
		salaryService.delete(id);
		return "list";
	}

	public String deletese() {
		if (ids != null && ids.length != 0) {
			for (int id : ids) {
				salaryService.delete(id);
			}
		}
		return "list";
	}

	// 分页显示所有
	public String findAll() {
		if (null == pagelist) {
			pagelist = new Page(1);
		}
		salaryService.findAll(pagelist);
		if (pagelist.getList().size() == 0) {
			return "noresult";
		}
		return "list";
	}

	// 修改前通过id获取记录
	public String findById() {
		if (null == id) {
			return "noresult";
		}

		pay = salaryService.findById(id);
		if (null == pay) {
			return "noresult";
		}
		return "payinput";
	}

	
	// 增加或者修改
	public String save() {
		if (null == id) {
			salaryService.add(pay);
		} else {
			salaryService.modify(pay);
		}
		return "list";
	}

	public String search() throws Exception {
		if (null == pagelist) {
			pagelist = new Page(1);
		}
		Map<String, String> sw = new HashMap<String, String>();
		if (null != minid) {
			sw.put("minid", String.valueOf(minid));
		}

		if (null != maxid) {
			sw.put("maxid", String.valueOf(maxid));
		}
		if (null != sname && !sname.isEmpty()) {
			sw.put("sname", GetSth.change(sname));
			// sw.put("sname", sname);
		}
		if (null != sminbase && !sminbase.isEmpty()) {
			sw.put("sminbase", sminbase);
			// throw new Exception("asdfasdf");
		}
		if (null != smaxbase && !smaxbase.isEmpty()) {
			sw.put("smaxbase", smaxbase);
		}
		if (null != sminage && !sminage.isEmpty()) {
			sw.put("sminage", sminage);
		}
		if (null != smaxage && !smaxage.isEmpty()) {
			sw.put("smaxage", smaxage);
		}
		if (null != sminpt && !sminpt.isEmpty()) {
			sw.put("sminpt", sminpt);
		}
		if (null != smaxpt && !smaxpt.isEmpty()) {
			sw.put("smaxpt", smaxpt);
		}
		salaryService.search(pagelist, sw);
		return "list";
	}

	// 通过员工id查找
	public String searchByEmId() {
		if (null == id) {
			return "noresult";
		}
		if (null == pagelist) {
			pagelist = new Page(1);
		}
		salaryService.searchByEmId(pagelist, id);
		return "list";
	}

	// 通过员工姓名查找
	public String searchByEmName() {
		if (null == sname) {
			return "noresult";
		}
		if (null == pagelist) {
			pagelist = new Page(1);
		}
		salaryService.searchByEmname(pagelist, GetSth.change(sname));
		return "list";
	}

	// 通过id查找
	public String searchById() {
		if (null == id) {
			return "noresult";
		}

		pay = salaryService.findById(id);
		if (null == pay) {
			return "noresult";
		}
		pagelist = new Page(1);
		pagelist.setPageTotalCount(1);
		List<Pay> list = new ArrayList<Pay>();
		list.add(pay);
		pagelist.setList(list);

		return "list";
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public void setIds(int[] ids) {
		this.ids = ids;
	}

	public void setMaxid(Integer maxid) {
		this.maxid = maxid;
	}

	public void setMinid(Integer minid) {
		this.minid = minid;
	}

	public void setPagelist(Page pagelist) {
		this.pagelist = pagelist;
	}

	public void setPay(Pay pay) {
		this.pay = pay;
	}

	public void setPays(List<Pay> pays) {
		this.pays = pays;
	}

	public void setSmaxage(String smaxage) {
		this.smaxage = smaxage;
	}

	public void setSmaxbase(String smaxbase) {
		this.smaxbase = smaxbase;
	}

	public void setSmaxpt(String smaxpt) {
		this.smaxpt = smaxpt;
	}

	public void setSminage(String sminage) {
		this.sminage = sminage;
	}

	public void setSminbase(String sminbase) {
		this.sminbase = sminbase;
	}

	public void setSminpt(String sminpt) {
		this.sminpt = sminpt;
	}

	public void setSname(String sname) {
		this.sname = sname;
	}
	public Integer getId() {
		return id;
	}

	public int[] getIds() {
		return ids;
	}

	public Integer getMaxid() {
		return maxid;
	}

	public Integer getMinid() {
		return minid;
	}

	public Page getPagelist() {
		return pagelist;
	}

	public Pay getPay() {
		return pay;
	}

	public List<Pay> getPays() {
		return pays;
	}

	public String getSmaxage() {
		return smaxage;
	}

	public String getSmaxbase() {
		return smaxbase;
	}

	public String getSmaxpt() {
		return smaxpt;
	}

	public String getSminage() {
		return sminage;
	}

	public String getSminbase() {
		return sminbase;
	}

	public String getSminpt() {
		return sminpt;
	}

	public String getSname() {
		return sname;
	}


}
